﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace Restaurant.DAO
{
    public class DoanhThuDB
    {
        private SqlConnection con = KetNoiDB.TaoKetNoi();

        public DataTable LoadDoanhThu()
        {
            con.Open();
            string strcmd = "dbo.spLoadDoanhThu";
            SqlCommand cmd = new SqlCommand(strcmd, con);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter adapt = new SqlDataAdapter(cmd);
            DataTable dbtb = new DataTable();
            adapt.Fill(dbtb);
            con.Close();
            return dbtb;
        }

        public DataTable LoadDoanhThuTheoThang()
        {
            con.Open();
            string strcmd = "dbo.spLayDoanhThuTheoThang";
            SqlCommand cmd = new SqlCommand(strcmd, con);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter adapt = new SqlDataAdapter(cmd);
            DataTable dbtb = new DataTable();
            adapt.Fill(dbtb);
            con.Close();
            return dbtb;
        }

        public DataTable LoadDoanhThuNgayNH(int MaNH, DateTime Ngay)
        {
            con.Open();
            string strcmd = "dbo.spLoadDoanhThuNgayNH";
            SqlCommand cmd = new SqlCommand(strcmd, con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@maNH", MaNH));
            cmd.Parameters.Add(new SqlParameter("@ngayTongKet", Ngay));
            SqlDataAdapter adapt = new SqlDataAdapter(cmd);
            DataTable dbtb = new DataTable();
            adapt.Fill(dbtb);
            con.Close();
            return dbtb;
        }

        public bool SuaDoanhThu(int maDoanhThu, int maNH, DateTime ngayTongKet, int tongTienBan)
        {
            try
            {
                con.Open();
                string strcmd = "dbo.spSuaDoanhThu";
                SqlCommand cmd = new SqlCommand(strcmd, con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@maDoanhThu", maDoanhThu));
                cmd.Parameters.Add(new SqlParameter("@maNH", maNH));
                cmd.Parameters.Add(new SqlParameter("@ngayTongKet", ngayTongKet));
                cmd.Parameters.Add(new SqlParameter("@tongTienBan", tongTienBan));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtb = new DataTable();
                adapt.Fill(dbtb);
                con.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool XoaDoanhThu(int ma)
        {
            try
            {
                con.Open();
                string strcmd = "dbo.spXoaDoanhThu";
                SqlCommand cmd = new SqlCommand(strcmd, con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@ma", ma));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtb = new DataTable();
                adapt.Fill(dbtb);
                con.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool ThemDoanhThu(int maNH, DateTime ngayTongKet, int tongTienBan)
        {
            try
            {
                con.Open();
                string strcmd = "dbo.spThemDoanhThu";
                SqlCommand cmd = new SqlCommand(strcmd, con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@maNH", maNH));
                cmd.Parameters.Add(new SqlParameter("@ngayTongKet", ngayTongKet));
                cmd.Parameters.Add(new SqlParameter("@tongTienBan", tongTienBan));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtb = new DataTable();
                adapt.Fill(dbtb);
                con.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool LapDoanhThuTheoNgay(int maNH, DateTime ngayTongKet)
        {
            try
            {
                con.Open();
                string strcmd = "dbo.spLapDoanhThuTheoNgay";
                SqlCommand cmd = new SqlCommand(strcmd, con);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add(new SqlParameter("@maNH", maNH));
                cmd.Parameters.Add(new SqlParameter("@ngayTongKet", ngayTongKet));
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataTable dbtb = new DataTable();
                adapt.Fill(dbtb);
                con.Close();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}